package com.example.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 异步消息配置
 */
@Configuration
@EnableCaching
public class AsyncMessageConfig {

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    // 配置线程池
    @Bean(name = "messageTaskExecutor")
    public ThreadPoolTaskExecutor messageTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);  // 核心线程数
        executor.setMaxPoolSize(20);  // 最大线程数
        executor.setQueueCapacity(100);  // 队列容量
        executor.setKeepAliveSeconds(60);  // 线程空闲时间
        executor.setThreadNamePrefix("Message-Thread-");  // 线程名前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  // 拒绝策略
        executor.initialize();
        return executor;
    }

    // 配置缓存管理器
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager(
            "messageCache",  // 消息缓存
            "userCache",     // 用户缓存
            "adminCache",    // 管理员缓存
            "dishCache",     // 菜品缓存
            "orderCache"     // 订单缓存
        );
    }

    // 管理员相关队列
    @Bean
    public Queue adminLoginQueue() {
        return new Queue("admin.login.queue", true);
    }

    @Bean
    public Queue adminRegisterQueue() {
        return new Queue("admin.register.queue", true);
    }

    @Bean
    public Queue adminUpdateQueue() {
        return new Queue("admin.update.queue", true);
    }

    @Bean
    public Queue adminPasswordQueue() {
        return new Queue("admin.password.queue", true);
    }

    // 菜品相关队列
    @Bean
    public Queue dishCreateQueue() {
        return new Queue("dish.create.queue", true);
    }

    @Bean
    public Queue dishUpdateQueue() {
        return new Queue("dish.update.queue", true);
    }

    @Bean
    public Queue dishDeleteQueue() {
        return new Queue("dish.delete.queue", true);
    }

    // 订单相关队列
    @Bean
    public Queue orderCreateQueue() {
        return new Queue("order.create.queue", true);
    }

    @Bean
    public Queue orderUpdateQueue() {
        return new Queue("order.update.queue", true);
    }

    @Bean
    public Queue orderPayQueue() {
        return new Queue("order.pay.queue", true);
    }

    @Bean
    public Queue orderCancelQueue() {
        return new Queue("order.cancel.queue", true);
    }

    // 评论相关队列
    @Bean
    public Queue commentCreateQueue() {
        return new Queue("comment.create.queue", true);
    }

    @Bean
    public Queue commentUpdateQueue() {
        return new Queue("comment.update.queue", true);
    }

    @Bean
    public Queue commentDeleteQueue() {
        return new Queue("comment.delete.queue", true);
    }

    // 学生健康相关队列
    @Bean
    public Queue healthUpdateQueue() {
        return new Queue("health.update.queue", true);
    }

    @Bean
    public Queue healthReportQueue() {
        return new Queue("health.report.queue", true);
    }

    // 食堂相关队列
    @Bean
    public Queue canteenUpdateQueue() {
        return new Queue("canteen.update.queue", true);
    }

    @Bean
    public Queue canteenStatusQueue() {
        return new Queue("canteen.status.queue", true);
    }

    // 学校相关队列
    @Bean
    public Queue schoolUpdateQueue() {
        return new Queue("school.update.queue", true);
    }

    @Bean
    public Queue schoolCanteenQueue() {
        return new Queue("school.canteen.queue", true);
    }
} 